/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.mallsite.modules.shop.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.common.mybatis.mapper.query.QueryWhere;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.utils.UserUtils;
import com.mallsite.modules.shop.entity.GoodsAttr;
import com.mallsite.modules.shop.entity.GoodsBrand;
import com.mallsite.modules.shop.service.GoodsAttrService;
import com.mallsite.modules.shop.utils.CoreUtils;

/**
 * 商品属性Controller
 * @author ruyang
 * @version 2020-12-10
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/goodsAttr")
public class GoodsAttrController extends BaseController {

	@Autowired
	private GoodsAttrService goodsAttrService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public GoodsAttr get(String attrId, boolean isNewRecord) {
		return goodsAttrService.get(attrId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("shop:goodsAttr:view")
	@RequestMapping(value = {"list", ""})
	public String list(GoodsAttr goodsAttr, Model model) {
		model.addAttribute("goodsAttr", goodsAttr);
		return "modules/shop/goodsAttrList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("shop:goodsAttr:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<GoodsAttr> listData(GoodsAttr goodsAttr, HttpServletRequest request, HttpServletResponse response) {
		if(!CoreUtils.isSysCodeDefault()) {
			QueryWhere qw = goodsAttr.getSqlMap().getWhere();
			String[] shopIds = {UserUtils.getUser().getRefCode(), "0"};
			qw.and("shop_id", QueryType.IN, shopIds);
		}
		Page<GoodsAttr> page = goodsAttrService.findPage(new Page<GoodsAttr>(request, response), goodsAttr); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("shop:goodsAttr:view")
	@RequestMapping(value = "form")
	public String form(GoodsAttr goodsAttr, Model model) {
		model.addAttribute("goodsAttr", goodsAttr);
		model.addAttribute("user", UserUtils.getUser());
		return "modules/shop/goodsAttrForm";
	}

	/**
	 * 保存商品属性
	 */
	@RequiresPermissions("shop:goodsAttr:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated GoodsAttr goodsAttr) {
		goodsAttrService.save(goodsAttr);
		return renderResult(Global.TRUE, text("保存属性成功！"));
	}
	
	@RequiresPermissions("shop:goodsAttr:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(GoodsAttr goodsAttr) {
		goodsAttrService.delete(goodsAttr);
		return renderResult(Global.TRUE, text("删除属性成功！"));
	}
	
}